package pl.us.ij.plugins.segmentation.forms;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import pl.us.ij.plugins.segmentation.commands.watershed.CombineSegmentationCommand;
import pl.us.ij.plugins.segmentation.commands.watershed.WatershedSegmentationCommand;
import pl.us.ij.plugins.segmentation.commands.watershed.WatershedSegmentationCommandParams;
import pl.us.ij.plugins.segmentation.infrastructure.CategoryInfo;
import pl.us.ij.plugins.segmentation.infrastructure.CommandParams;
import pl.us.ij.plugins.segmentation.localization.Localization;

import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-05-26
 * Time: 00:35:34
 * To change this template use File | Settings | File Templates.
 */
public class WatershedForm extends CategoryInfo {
    private JPanel watershedPanel;
    private JComboBox watershedMaskSizeComboBox;
    private JButton segmentationButton;
    private JButton combineWithOriginalButton;

    public WatershedForm(IActionHandler actionHandler) {

        actionHandler.setAction(segmentationButton, WatershedSegmentationCommand.class, getWatershedSegmentationParams(false));
        actionHandler.setAction(combineWithOriginalButton, CombineSegmentationCommand.class, null);
    }

    @Override
    public String getTitle() {
        return Localization.getString("watershedForm.title");
    }

    @Override
    public JPanel getPanel() {
        return watershedPanel;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getDescription() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    private ICommandParamGather getWatershedSegmentationParams(final boolean overlapPicture) {
        return new ICommandParamGather() {

            public CommandParams getCommandParams() {
                WatershedSegmentationCommandParams params = new WatershedSegmentationCommandParams();

                params.setOverlapPicture(overlapPicture);

                return params;
            }
        };
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        watershedPanel = new JPanel();
        watershedPanel.setLayout(new FormLayout("fill:d:noGrow", "center:d:noGrow"));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:noGrow,left:4dlu:noGrow,fill:75px:noGrow,left:4dlu:noGrow,fill:180px:noGrow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        CellConstraints cc = new CellConstraints();
        watershedPanel.add(panel1, cc.xy(1, 1));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("watershedForm.maskSizeLabel"));
        panel1.add(label1, cc.xy(1, 1));
        watershedMaskSizeComboBox = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("3");
        defaultComboBoxModel1.addElement("5");
        defaultComboBoxModel1.addElement("7");
        defaultComboBoxModel1.addElement("9");
        defaultComboBoxModel1.addElement("11");
        defaultComboBoxModel1.addElement("13");
        defaultComboBoxModel1.addElement("15");
        defaultComboBoxModel1.addElement("17");
        defaultComboBoxModel1.addElement("19");
        watershedMaskSizeComboBox.setModel(defaultComboBoxModel1);
        panel1.add(watershedMaskSizeComboBox, cc.xy(3, 1));
        segmentationButton = new JButton();
        this.$$$loadButtonText$$$(segmentationButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("watershedForm.segmentizeLabel"));
        panel1.add(segmentationButton, cc.xyw(1, 3, 3));
        combineWithOriginalButton = new JButton();
        this.$$$loadButtonText$$$(combineWithOriginalButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("watershedForm.combineWithImage"));
        panel1.add(combineWithOriginalButton, cc.xy(5, 3));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return watershedPanel;
    }
}
